import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;

public class TreeSet自然排序 {
    public static void main(String[] args) {
        TreeSet set=new TreeSet();

        set.add(new user("xc",29));
        set.add(new user("lmf",21));
        set.add(new user("Jom",20));
        set.add(new user("Jack",31));

        Iterator iter= set.iterator();
        while (iter.hasNext()){
            System.out.println(iter.next());
        }
    }
}

class user implements Comparable{
    private String name;
    private int age;

    public user() {
    }

    public user(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "user{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        user user = (user) o;
        return age == user.age && name.equals(user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof user) {
            user user=(user) o;
            return name.compareTo(user.name);
        }else{
            throw new RuntimeException("输入数据类型不匹配。");
        }
    }
}